#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "modes.h"
#include "structs.h"

char *get_argument(char *name, int num_args, struct arguments *args)
{
	int i;
	for (i = 0; i < num_args; i++)
		if (strcmp(args[i].name, name) == 0)
			return args[i].value;
	return (void *)0;
}

PLUGIN(sleep_constant)
{
	time_t right_now;
	char *timeout;
	struct command *com = (struct command *)command;
	system(com->command);
	right_now = time(NULL);
	timeout = get_argument("delay", com->args->length, com->args->args);
	if (timeout == (void *)0) {
		com->wake = right_now + 10;
	} else {
		com->wake = right_now + atoi(timeout);
	}

	return 0;
}

PLUGIN(sleep_duty)
{
/*    if ((command->mode->found_arguments) && (command->mode->found_arguments == 1))
    {
	int tmp = atoi(command->mode->arg_values[0]->value);
	return (((100.0 - tmp) * command->history_points[0].ran) / tmp);
    }
*/
    return (void *)10;
}

PLUGIN(sleep_target)
{
    return (void *)10;
}

struct mode modes[] = {
    {"constant", "constant time", 1, "s", &sleep_constant},
    {"duty", "duty cycle", 1, "d", &sleep_duty},
    {"target", "target time", 1, "t", &sleep_target}
};

int get_mode_by_name(char *name)
{
	int i;

	for (i = 0; i < NUM_MODES; i++)
	{
		if (strcmp(name, modes[i].name) == 0)
		{
			return i;
		}
	}

	return -1;
}
